"""
Case Type   : 服务端工具
Case Name   : 验证gs_dump工具功能
Create At   : 2024.10.8
Owner       : wang-tianjing1
Description :
    1.显示版本号信息:
    2.导出数据库
    3.清理环境
Expect      :
    1.显示正确
    2.成功
    3.成功
History     :
"""
import unittest
import os
from yat.test import Node
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger



class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_server_tools(self):
        self.log.info('-------step1:显示版本号信息;except:显示正确---------')
        cmd_list = ['-V', '--version']
        for cmd in cmd_list:
            check_cmd1 = f'''source {macro.DB_ENV_PATH}
                        gs_dump {cmd}'''
            self.log.info(check_cmd1)
            msg1 = self.primary_node.sh(check_cmd1).result()
            self.log.info(msg1)
            self.assertIn('openGauss', msg1)

        self.log.info('--------step2:导出数据库;except:成功--------')
        self.log.info('---------连接数据库并创建数据库---------')
        sql_cmd1 = '''drop database if exists testdump;
                    create database testdump;
                    '''
        excute_cmd1 = f'source {macro.DB_ENV_PATH};' \
                      f'gsql -d {self.primary_node.db_name} ' \
                      f'-p {self.primary_node.db_port} ' \
                      f'-c "{sql_cmd1}";'
        self.log.info(excute_cmd1)
        msg1 = self.primary_node.sh(excute_cmd1).result()
        self.log.info(msg1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, msg1)
        self.log.info('---------在创建好的数据库中创建表并插入数据----------')
        sql_cmd2 = '''drop table if exists test1; 
                    create table test1 (id int ,name char(10));
                    insert into test1 values (1,'aa'),(2,'bb');
                    '''
        excute_cmd2 = f'source {macro.DB_ENV_PATH};' \
                      f'gsql -d testdump ' \
                      f'-p {self.primary_node.db_port}' \
                      f' -c "{sql_cmd2}";'
        self.log.info(excute_cmd2)
        msg2 = self.primary_node.sh(excute_cmd2).result()
        self.log.info(msg2)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg2)
        self.log.info('----------导出数据库时指定verbose模式---------')
        excute_cmd3 = f'''source {macro.DB_ENV_PATH} ;
                    gs_dump -p {self.primary_node.db_port} testdump -v
                    '''
        self.log.info(excute_cmd3)
        msg3 = self.primary_node.sh(excute_cmd3).result()
        self.log.info(msg3)
        self.assertIn(self.constant.GS_DUMP_SUCCESS_MSG, msg3)

    def tearDown(self):
        self.log.info('---------step3:清理环境：删除数据库;except:成功----------')
        sql_cmd4 = '''drop database if exists testdump; '''
        excute_cmd4 = f'source {macro.DB_ENV_PATH};' \
                      f'gsql -d {self.primary_node.db_name} ' \
                      f'-p {self.primary_node.db_port} ' \
                      f'-c "{sql_cmd4}";'
        self.log.info(excute_cmd4)
        msg4 = self.primary_node.sh(excute_cmd4).result()
        self.log.info(msg4)
        self.log.info(f'-----{os.path.basename(__file__)} end------')